<template>
  <div class="history-nav">
      <div class="title">历史留言</div>
      <!-- 返回按钮 -->
      <img src="../../assets/images/jiantou_left.png" alt="返回按钮" class="back-button" @click="goBack" />
  </div>
  <div class="history-list">
    <div v-for="feedback in feedbacks" :key="feedback.id" class="feedback-item" @click="showDetail (feedback)">
      <div class="feedback-info">
        <div class="feedback-type"> {{ feedback.category }}</div>
        <div class="feedback-time">反馈时间: {{ feedback.createdAt }}</div>
      </div>
      <div :class="['feedback-status', { 'accepted': feedback.status === '已处理' }]">{{ feedback.status }}</div>
    </div>
  </div>

  <div v-if="showDialog" class="dialog-wrapper">
    <div class="dialog-background" @click="showDialog = false"></div>
    <div class="content-background">
      <h3>留言详情</h3>
      <img src="../../assets/images/OrderDetailback.png" alt="" class="back-person">
      <div class="dialog-content">
        <div class="detail-item">
            <div class="orange">
              <div class="label">反馈类型：</div>
            </div>
            <span class="value">{{ selectedFeedback.category }}</span>
        </div>
        <div class="detail-item">
            <div class="orange">
              <div class="label">反馈状态：</div>
            </div>
            <span class="value">{{ selectedFeedback.status }}</span>
        </div>
        <div class="detail-item">
            <div class="orange">
              <div class="label">反馈时间：</div>
            </div>
            <span class="value">{{ selectedFeedback.createdAt }}</span>
        </div>
        <div class="detail-item">
            <div class="orange">
              <div class="label">用户反馈：</div>
            </div>
            <span class="value">{{ selectedFeedback.message }}</span>
        </div>
        <div class="detail-item">
            <div class="orange">
              <div class="label">回复结果：</div>
            </div>
            <span class="value">{{ selectedFeedback.response }}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {ref,reactive,onMounted} from "vue"
import { useRouter } from "vue-router";
 import {  showToast } from 'vant';
import { getFeedbacks } from "../../api/Feedback/index";

const router = useRouter();

const feedbacks = ref([]);
const showDialog = ref<boolean>(false);
  const selectedFeedback = reactive({
  category: "",
  status: "",
  createdAt: "",
  message: "",
  response:""
});

// 从本地存储获取用户ID
const userId = ref<number | null>(null);
onMounted(async () => {
  const storedUser = localStorage.getItem('user');
  if (storedUser) {
    const userData = JSON.parse(storedUser).user;
    userId.value = userData.userId;

    // 获取历史反馈信息
    if (userId.value !== null) {
      try {
        const response = await getFeedbacks(userId.value);
        if (response.data.code === 200) {
          feedbacks.value = response.data.data;
        } else {
          showToast("获取历史反馈失败：" + response.data.message);
        }
      } catch (error) {
        console.error("获取历史反馈失败:", error);
        showToast("获取历史反馈失败，请稍后再试");
      }
    }
  }
});
const showDetail = (feedbacks: any) => {
  selectedFeedback.category = feedbacks.category;
  selectedFeedback.status = feedbacks.status;
  selectedFeedback.createdAt = feedbacks.createdAt;
  selectedFeedback.message = feedbacks.message;
  selectedFeedback.response = feedbacks.response;
  showDialog.value = true;
};
// 返回到上一个页面
const goBack = () => {
  router.back();
};
</script>
<style lang="less" scoped>
.history-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  height: 50px; 
}
.title {
  font-size: 18px; 
  margin-top: 10px;
  text-align: center;
}
.back-button {
  position: absolute;
  left: 10px; 
  width: 34px; 
  height: 34px; 
  margin-top: 10px;
}

.history-list {
  padding: 20px;
}

.feedback-item {
  display: flex;
  justify-content: space-between;
  align-items: center; /* 上下居中 */
  border-radius: 20px;
  padding: 10px;
  margin-bottom: 10px;
  background-color: #fff;
}

.feedback-info {
  display: flex;
  flex-direction: column;
}
.feedback-status{
  width: 68px;
  height: 41px;
  background: #FF8C00;
  color: #fff;
  text-align: center;
  align-items: center;
  justify-content: center;
  display: flex;
  border-radius: 15px;
  
}
.feedback-status.accepted {
  background: #FD5958; 
}

.dialog-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.dialog-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5); /* 半透明黑色背景 */
}

.dialog-content {
  position:absolute;
  top: 65px;
  left: 10px;
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  z-index: 1001;
  width: 80%;
  max-width: 400px;
  height: 275px;
}
.detail-item {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  align-items: center;
}

.orange{
  width: 100px;
  height: 30px;
  background: #FFCB87;
  border-radius: 10px;
}
.label {
  font-weight: bold;
  color: #FFFFFF;
  margin-left: 15px;
  margin-top: 5px;
}

.value {
  width: 128px;
  color: #333;
}

.content-background{
  position: relative;
  width: 300px;
  height: 335px;
  background-color: #FFBF6B;
  border-radius: 40px 16px 16px 16px;
  padding-top: 65px;
}
.content-background h3{
  color: #D37902;
  font-size: 24px;
  width: 100px;
  position:absolute;
  top: 20px;
  left: 40px;
}

.back-person{
  width: 183px;
  height: 119px;
  position: absolute;
  top: -40px;
  left: 160px;
}
</style>
